$(function() {
	
	var phoneNum=$("#phoneMana");
	var len = $("#len");
	phoneNum.blur(function(){
		//alert("遗失光标");
		var phone=$("#phoneMana").val();
		if(phone.length<11)
		{
			len.html("电话号码不足十一位").css({color: "red" });
			phoneNum.focus();
		}else if (phone.length>11) {
			len.html("电话号码大于十一位").css({color: "red" });
			phoneNum.focus();
		}else if (phone.substr(0,1)!=1){
			len.html("电话号码不是以1开头").css({color: "red" });
			phoneNum.focus();
		}else{
			len.html("Ok!").css({color: "blue" });
		}

		});
	
	//提交信息修改按钮
	var modifyInfoButn=$("#modifyInfo");
	modifyInfoButn.click(function(){
		//alert("modify");
		$.ajax({
			type:"post",
			url:"../userInfo/updateUserInfo",
			data:{
			
				userAccount:$("#userAccountMana").val(),
				nickname:$("#nickNameMana").val(),
				detailsId:$("#userDetailsId").val(),
				gender:$("#genderMana").val(),
				address:$("#addressMana").val(),
				phone:$("#phoneMana").val(),
				realname:$("#realNameMana").val(),
				addAddress:$("#addAddressMana").val()
			},
			dataType:"json",
			success:function(){
				alert("修改成功");
				window.location.href="../userInfo/getUserInfo";
			},
			error:function(){
				alert("修改异常");
			}
		})
		});

	//密码修改校验
	var oldpsw=$("#oldPW");
	var psw1= $("#psw1");
	
	oldpsw.blur(function(){
		var oldVal=$("#oldPW").val();
			if(oldVal.length<6)
		{
			psw1.html("密码长度不足六位！").css({ color:"red"});	
		
		}else if(oldVal.length>6){
			
				psw1.html("密码长度大于六位！").css({ color:"red"});	
		}else{
			psw1.html("OK！").css({ color:"blue"});
		}
	});

	var newpsw=$("#newPW");
	var psw2=$("#psw2");
	newpsw.blur(function(){
		var newVal=$("#newPW").val();
		if(newVal.length<6){
			psw2.html("密码长度不足六位！").css({ color:"red"});
			
		}else if (newVal.length>6) {
			psw2.html("密码长度大于六位！").css({ color:"red"});
		}else{
			psw2.html("OK！").css({ color:"blue"});
		}
	});
	var checkpsw=$("#checkPW");
	
	var psw3=$("#psw3");
	checkpsw.blur(function(){

		var newVal=$("#newPW").val();
		var checkVal=$("#checkPW").val();
		
		if (newVal!=checkVal) {
			psw3.html("两次输入的密码不一样！").css({ color:"red"});
		}else
		{
			psw3.html("OK！").css({ color:"blue"});
		}

	})
	
	//取消密码修改按钮
	var resetPWButn=$("#resetPWButn");
	resetPWButn.click(function(){
		//alert("quiao");
		newpsw.val("");
		oldpsw.val("");
		checkpsw.val("");
		psw1.html("");
		psw2.html("");
		psw3.html("");
	})
	//提交密码修改按钮
	var modifyPWButn=$("#modifyPWButn");
	modifyPWButn.click(function(){ 
		$.ajax({
			type:"post",
			url:"../userInfo/modifyPW",
			data:{
				oldpsw:MD5($("#oldPW").val()),
				newpsw:MD5($("#newPW").val())
			},
			dataType:"json",
			success:function(data){
				if(data==0){
					alert("用户名出现异常");
				}else if(data==2){
					alert("密码不匹配，修改不成功")
				}else if(data==1){
					alert("密码修改成功");
					window.location.href="../userInfo/getUserInfo";
				}
			},
			error:function(){
				alert("修改异常");
			}
		
		})
	})

	// 查询订单信息
	//var selectOrder=$("#selectOrder");
	//selectOrder.click(function(){
	var getOrder=$("#getOrder");
	getOrder.click(function(){
		$.ajax({
			type:"post",
			url:"../userInfo/getOrderInfo",
			data:{},
			dataType:"json",
			success:function(list){
				//商品详情功能
				$("tbody tr").remove();
				for(var i in list ){
					var tr=$("<tr></tr>");
					var td=$("<td></td>");
					var a=document.createElement("a");
					a.innerText="商品详情";
					a.onclick=function(){
						//alert("进行查询操作");
						$.ajax({
							type:"post",
							url:"../userInfo/getGoodsInfo",
							data:{
								saleGoodsId:list[i].saleGoodsId
							},
							dataType:"json",
							success:function(){
								//alert("操作正常");
								 var url="../pages/user/userGoodsOrderInfo.jsp";
					                var n="商品详情";		           
					                var s="resizable:no;help:no;status:no;scroll:no";
								    window.open(url,n,"width=880,height=550",s);
							},
							error:function(){
								alert("操作异常");
							}
						})
					};
					//删除功能
					var td2=$("<td></td>");
					var a2=document.createElement("a");
					a2.innerText="删除";
					a2.onclick=function(){
						$.ajax({
							type:"post",
							url:"../userInfo/deleteOrder",
							data:{id:list[i].id},
							dataType:'json',
							success:function(){
								alert("删除成功");
								window.location.href="../userInfo/getUserInfo";
							},
							error:function(){
								alert("删除异常");
							}
						})
					};
					td.append(a);
					td2.append(a2)
					tr.append("<td>"+list[i].id+"</td>");
					tr.append(td);
					tr.append("<td>"+list[i].singlePrice+"</td>");
					tr.append("<td>"+list[i].num+"</td>");
					tr.append("<td>"+list[i].sumPrice+"</td>");
					tr.append("<td title='"+list[i].recPhone+"'>"+list[i].recPhone+"</td>");
					tr.append("<td>"+list[i].recAddress+"</td>");
					tr.append("<td>"+list[i].recName+"</td>");
					if(list[i].state==1){
						tr.append("<td>已发货</td>");
					}else{
						tr.append("<td>待发货</td>");
					}
					tr.append(td2);
					$("tbody").append(tr);
				}

			},
			error:function(){
				alert("执行异常");
			}
		})
		
	})
	
})	

